Repository navigation
Conversation
marshalDecimal rounded d/0.001 in binary, so a tie like 0.5015 (stored as 0.50149999...) went down and 2.0005 (stored as 2.00050000...) went up. RFC 9651 section 4.1.5 rounds the decimal number, and the official test suite now has cases for this. Round the shortest decimal form of the float instead. Also fail on NaN and infinities (NaN was written out as "NaN") and drop the sign when a negative value rounds to zero. Bump structured-field-tests to 10781e4 (same as dunglas#27), which has the new tie-to-even cases.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
marshalDecimalroundsd/0.001withmath.RoundToEven, so the tie is decided on the binary value of the float. RFC 9651 section 4.1.5 rounds the decimal number half to even:The official suite added these cases in httpwg/structured-field-tests#114, which is why the dependabot bump in #27 fails CI (4 serialization tests).
The fix rounds the shortest decimal form of the float (
strconv.FormatFloat(d, 'f', -1, 64)) as a string, half to even. Two small things in the same function:NaNwas written out asNaN; NaN and infinities now returnErrInvalidDecimal.-0.0001) gave-0.0; it now gives0.0, since the sign is only added when the rounded value is below zero.This also bumps
structured-field-teststo 10781e4, the same commit as #27, so the official tests cover it.Testing:
TestMarshalDecimal; 6 of them fail on main and pass now.go test ./...,go vetand golangci-lint are clean..xxx5ties) against Python'sDecimal(repr(x)).quantize(Decimal("0.001"), ROUND_HALF_EVEN): main differs on 14,408, this branch on 0.